﻿#include "page_binom.h"
#include "ui_page_binom.h"
#include <cmath>
#include <vector>
#include "m_combinatorics.h"



//================================ Вова ================================

double expectedvalue(unsigned int n, double p)
{return n*p;}
double dispersion(unsigned int n, double p)
{return n*p*(1.0-p);}
double sqrt_dispersion(unsigned int n, double p)
{return sqrt(n*p*(1.0-p));}

double binomial(unsigned int n, double p, unsigned int k)
{
    return m_combinatorics(k, n).C()*pow(p, (int)k)*pow((1.0-p), (int)(n-k));
}


std::vector<double> distribution(unsigned int n, double p)
{
    std::vector<double> result;
    for (unsigned int i = 0; i <= n; ++i)
    {
        result.push_back(binomial(n, p, i));
    }

    return result;
}

//================================================================

page_binom::page_binom(QWidget *parent)
	: page_clean(parent) // заменить QWidget на page_clean
{
	ui = new Ui::page_binomClass();
	ui->setupUi(this);

    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_disp, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_f_raspr, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_sr_kv_otkl, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_math_o, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_rad_raspr, SLOT(setChecked(bool)));
}

page_binom::~page_binom()
{
    delete ui;
}

void page_binom::inputBinom(unsigned int *n, double *p)
{
    (*n) = ui->input_n->value();
    (*p) = ui->input_p->value();
}


void page_binom::on_calc_btn_clicked()
{
    unsigned int n;
    double p;
    inputBinom(&n, &p);
    std::vector<double> v = distribution(n, p);

    if(ui->checkBox_rad_raspr->checkState()==Qt::Checked)
    {
        show_Rad(v);
    }
    if(ui->checkBox_math_o->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("M(X) = ")+QString::number( expectedvalue(n, p) ));
    }
    if(ui->checkBox_disp->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("D(X) = ")+QString::number( dispersion(n, p) ));
    }
    if(ui->checkBox_sr_kv_otkl->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("&#963;(X) = ")+QString::number( sqrt_dispersion(n, p) ));
    }
    if(ui->checkBox_f_raspr->checkState()==Qt::Checked)
    {
        show_Graph(v);
    }
    //if(ui->checkBox_mng->checkState()==Qt::Checked)           //calc_Mng();
    //if(ui->checkBox_graph->checkState()==Qt::Checked)
    emit appendToLog("<hr />");
}
